package JAVA241128;

class MySingleList {

    public class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public int listSize;

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
        listSize++;
        /*if(head == null) {
            head = node;
        }else {
            //对于插入来说 我们一般建议 先绑定后边
            node.next = head;
            head = node;
        }*/
    }
    public void show() {
        ListNode cur = head;
        while (cur != null) {
            System.out.println(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public ListNode reverseList() {
            if (head == null) {
                return null;
            }
            if (head.next == null) {
                return head;
            }
            ListNode cur = head.next;
            head.next = null;
            while (cur != null) {
                ListNode curNext = head;
                cur.next = head;
                head = cur;
                cur = curNext;
            }
            return head;
        }
    }
class Test{
    public static void main(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addFirst(12);
        mySingleList.addFirst(23);
        mySingleList.addFirst(34);
        mySingleList.addFirst(45);
        mySingleList.addFirst(56);
        mySingleList.show();
        MySingleList.ListNode ret = mySingleList.reverseList();
        mySingleList.show();
    }
}
